<?php
/**
 * 常用的静态方法
 */
class Qutil{
	
	/**
	 * 过滤数据
	 * @param string|array $data ($_GET,$_POST,$_COOKIE)
	 * @param string $type int|float
	 * @param bool $isnum true|false
	 */
	static function filter($str, $type = ''){
		if(is_array($str)){
			foreach($str as $key => $val) {
				$str[$key] = self::filter($val, $type);
			}
			return $str;
		}
		switch ($type){
			case 'int':
				$str = intval($str);
				break;
			case 'float':
				$str = floatval($str);
				break;
			default:
				$str = htmlspecialchars($str, ENT_QUOTES);
		}
		return $str;
	}

	/**
	 * 获取ip
	 */
	static function getIP($long=false) {
		$xip = getenv('HTTP_X_FORWARDED_FOR');
		$rip = getenv('REMOTE_ADDR');
		$srip = $_SERVER['REMOTE_ADDR'];
		if($xip && strcasecmp($xip, 'unknown')) {
			$ip = $xip;
		} elseif($rip && strcasecmp($rip, 'unknown')) {
			$ip = $rip;
		} elseif($srip && strcasecmp($srip, 'unknown')) {
			$ip = $srip;
		}
		preg_match("/[\d\.]{7,15}/", $ip, $match);
		$ip = $match[0] ? $match[0] : 'unknown';
		if($long){
			return sprintf("%u",ip2long($ip));
		}
		return $ip;
	}
	
	/**
	 * 检查邮箱格式
	 */
	static function validEmail($email){
		if( preg_match('/^[a-z0-9-_.]+@[\da-z][\.\w-]+\.[a-z]{2,4}$/i', $email) ){
			return true;
		}
		return false;
	}
	
	/** 
	 * 检查QQ格式
	 * @return boolean
	 */ 
	static function IsQQ($Argv){ 
		$RegExp='/^[1-9][0-9]{5,11}$/'; 
		return preg_match($RegExp,$Argv) ? true : false; 
	} 

	/** 
	 * 检查手机格式
	 * @return boolean
	 */ 
	static function IsMobile($Argv){ 
		$RegExp='/^(?:13|15|18|14)[0-9]\d{8}$/';
		return preg_match($RegExp,$Argv) ? true : false;
	}
	
	/**
	 * 判断一个字符串中是否只包含了 数字和逗号(sql in查询的时候使用)
	 * @param string $string
	 * @return boolean
	 */
	static function onlyNumAndComma($string){
		if(preg_match('/[^\d,]+/', $string)){
			return false;
		}
		return true;
	}

	/**
	 * 获取随机数
	 * @param int $len 长度
	 * @param int $type 1数字 2字母 0混合
	 */
	static function getRandom($len, $type=0) {
		if($type == 0) {
			$chars = '0123456789abcdefghijklmnopqrstuvwxyz';
		} else if($type == 1) {
			$chars = '0123456789';
		} else if($type == 2) {
			$chars = 'abcdefghijklmnopqrstuvwxyz';
		}
		$max = strlen($chars) - 1;
		mt_srand((double)microtime() * 1000000);
		for($i = 0; $i < $len; $i++){
			$hash.= $chars[mt_rand(0, $max)];
		}
		return $hash;
	}
	
	/**
	 * 获取时间差
	 * @param int $start_time 开始时间
	 * @param int $end_time 结束时间
	 * @return array
	 */
	static function timediff($start_time,$end_time=0){
		if($start_time > $end_time){
			$arr = array("day" => 0,"hour" => 0,"min" => 0,"sec" => 1);
		} else{
			$timediff = $end_time-$start_time;
			$days = intval($timediff/86400);
			$remain = $timediff%86400;
			$hours = intval($remain/3600);
			$remain = $remain%3600;
			$mins = intval($remain/60);
			$secs = $remain%60;
			$arr = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs);
		}
		return $arr;
	}
	
	/**
	 * 设置cookie
	 * @param array $array
	 * @param int $life 过期时间单位秒
	 * @param $path 设置作用路径
	 * @param $domain 设置作用域名
	 */
	static function ssetcookie($array, $life=0, $path = '/', $domain = '') {
		$_array = array_keys($array);
		for($i = 0; $i < count($array); $i++) {
			$httpOnly = in_array($_array[$i], array('skey','adminKey')) ? true : false;
			setcookie($_array[$i], $array[$_array[$i]], $life ? (time()+$life) : 0, $path, $domain, '', $httpOnly);
		}
	}
	
	/**
	 * 双向加密解密
	 * @param string $string
	 * @param string $operation
	 * @param mix $key
	 * @param int $expiry
	 * @return string
	 */
	static function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
		$ckey_length = 4;
		$key = md5($key ? $key : core_lib_constant::KEY);
		$keya = md5(substr($key, 0, 16));
		$keyb = md5(substr($key, 16, 16));
		$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
		$cryptkey = $keya . md5($keya . $keyc);
		$key_length = strlen($cryptkey);
		$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
		$string_length = strlen($string);
		$result = '';
		$box = range(0, 255);
		$rndkey = array();
		for ($i = 0; $i <= 255; $i++) {
			$rndkey[$i] = ord($cryptkey[$i % $key_length]);
		}
		for ($j = $i = 0; $i < 256; $i++) {
			$j = ($j + $box[$i] + $rndkey[$i]) % 256;
			$tmp = $box[$i];
			$box[$i] = $box[$j];
			$box[$j] = $tmp;
		}
		for ($a = $j = $i = 0; $i < $string_length; $i++) {
			$a = ($a + 1) % 256;
			$j = ($j + $box[$a]) % 256;
			$tmp = $box[$a];
			$box[$a] = $box[$j];
			$box[$j] = $tmp;
			$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
		}
		$_str = '';
		if ($operation == 'DECODE') {
			if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
				$_str = substr($result, 26);
			}
		} else {
			$_str = $keyc . str_replace('=', '', base64_encode($result));
		}
		return $_str;
	}
	
	static function enId($id){
		return $id*200;
	}
	
	static function deId($id){
		return intval($id/200);
	}
	
	static function mk_dir($dir, $mode = 0777) {
	    if (is_dir($dir) || @mkdir($dir, $mode))
	        return true;
	    if (!self::mk_dir(dirname($dir), $mode))
	        return false;
	    return @mkdir($dir, $mode);
	}
	
	static function isUsername($str){
		if(preg_match("/^[\x80-\xffa-zA-Z0-9]{3,60}$/", $str)){
			return true;
		}
		return false;
	}
}